mini.parse();

function ArticlesHandler() {
};

ArticlesHandler.instance = new ArticlesHandler();

ArticlesHandler.isQueryBtnExpended = false;

//获得全局的文章数据表格对象
ArticlesHandler.articlesGrid = mini.get("articlesGrid");

//加载表格数据
ArticlesHandler.articlesGrid.load();

//品牌状态数据
ArticlesHandler.brandStateData = [
	{text: '全部', id: -1},
	{text: '无效', id: 0},
	{text: '有效', id: 1}
];

mini.get("query_baiState").setData(ArticlesHandler.brandStateData);

/**
 * 根据用户权限展示功能按钮
 */
ArticlesHandler.loadToolbar = function() {
	var array = new Array("articles_list_add", "articles_list_edit", "articles_list_del","articles_list_query","articles_list_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
ArticlesHandler.loadToolbar();

/**
 * 弹出添加文章窗口
 */
ArticlesHandler.addArticles = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(780, 480, "添加文章");
	miniWin.setOption("bodyStyle","padding:0px;");
	miniWin.setOption("showMaxButton",true);
	miniWin.setOnButtonClick();

	miniWin.load("articlesForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new"};
        iframe.contentWindow.ArticlesFormHandler.passData(data);
        miniWin.setOnMax(iframe.contentWindow.ArticlesFormHandler.onMaxAction);
	},function(action){
		ArticlesHandler.articlesGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑文章窗口
 */
ArticlesHandler.editArticles = function() {
	var rowData = ArticlesHandler.articlesGrid.getSelected();
	var rowsData = ArticlesHandler.articlesGrid.getSelecteds();
	
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(780, 480, "编辑文章[<font color='blue'>"	+ rowData.baiTitle + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		miniWin.setOption("showMaxButton",true);
		miniWin.setOnButtonClick();
		
		miniWin.load("articlesForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", baiId: rowData.baiId};
            iframe.contentWindow.ArticlesFormHandler.passData(data);
            miniWin.setOnMax(iframe.contentWindow.ArticlesFormHandler.onMaxAction);
		},function(action){
			ArticlesHandler.articlesGrid.reload();
		});
		
		miniWin.show();
	}else if (rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else {
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 删除文章资源
 */
ArticlesHandler.delArticles = function() {
	if (!MiniuiTools.hasPurview("articles_list_del")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	var rowsData = ArticlesHandler.articlesGrid.getSelecteds();
	
	if(rowsData.length > 0){
		mini.confirm("确认删除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].baiId+ ",";
					}
					ids += rowsData[rowsData.length - 1].baiId;
					
					ArticlesHandler.articlesGrid.loading();
					
                    $.ajax({
                        url: __root + "/delBaiByIds.do",
                        data: {
				        	baiIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		ArticlesHandler.articlesGrid.reload();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
                        },
                        complete: function(){
                        	ArticlesHandler.articlesGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 前端预览文章
 */
ArticlesHandler.viewPointArticle = function(){
	var msgId = mini.loading("功能实现中，敬请期待 ...", "系统提示");
 	setTimeout(function () {mini.hideMessageBox(msgId);}, 2000);
    return;
}

/**
 * 更新文章状态
 */
ArticlesHandler.updateArticlesState = function() {
	if (!MiniuiTools.hasPurview("articles_list_edit")) {
		return;
	}
	
	var rowData = ArticlesHandler.articlesGrid.getSelected();
	var baiId = rowData.baiId;
	
	ArticlesHandler.articlesGrid.loading();
	
    $.ajax({
        url: __root + "/updateBaiStateById.do",
        data: {
        	baiId: baiId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            ArticlesHandler.articlesGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	ArticlesHandler.articlesGrid.unmask();
        }
    });	
};

/**
 * 展开或折叠查询文章面板
 */
ArticlesHandler.queryArticles = function() {
	if (ArticlesHandler.isQueryBtnExpended) {
		ArticlesHandler.isQueryBtnExpended = false;
		mini.get("articles_list_query").setText("展开查询");
		mini.get("articles_list_query").setIconCls("icon-btn-expand");
	} else {
		ArticlesHandler.isQueryBtnExpended = true;
		mini.get("articles_list_query").setText("折叠查询");
		mini.get("articles_list_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryArticlesDiv").toggle(500);
    setTimeout(function(){mini.get("articlesPanel").doLayout();},600);
};

/**
 * 执行查询文章的方法
 */
ArticlesHandler.doQueryArticles = function(){
	var queryBagTree = mini.get("queryBagTree").getValue();
	var baiTitle = mini.get("query_baiTitle").getValue();
	var baiState = mini.get("query_baiState").getValue();
	
    ArticlesHandler.articlesGrid.load({baiGroup: queryBagTree, baiTitle: baiTitle, baiState: baiState});
};

/**
 * 刷新页面数据及布局
 */
ArticlesHandler.afresh = function(){
	ArticlesHandler.doQueryArticles();
	mini.get("articlesPanel").doLayout();
}

/**
 * DrawCell 绘制单元格
 */
ArticlesHandler.articlesGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;
     
     if (column.field == "baiTime") {
     	if(e.value != ""){
     		var currentDateTime = XMAppUtils.changeDateToDateTimeStr(e.value);
			e.cellHtml = currentDateTime;
     	}else{
     		e.cellHtml = "";
     	}
     }
     
     if (column.field == "baiState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='ArticlesHandler.updateArticlesState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='ArticlesHandler.updateArticlesState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
     }
     
     if (column.field == "baiOption") {
     	e.cellHtml = "<span class='icon-show-view' style='cursor: pointer;' onclick='ArticlesHandler.viewPointArticle()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>" 
    			   + "<span class='icon-show-trash' style='cursor: pointer;' onclick='ArticlesHandler.delArticles()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
     }
});

